package cn.flyboy.test;

import cn.flyboy.entity.Teacher;
import cn.flyboy.mapper.TeacherMapper;
import cn.flyboy.utils.ExcelWriter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class testReadExcel {
    @Resource
    private TeacherMapper teacherMapper;

    @Test
    public void testExcel() throws Exception {

        String fileUrl = "F:\\OnlineExam\\src\\files\\student.xlsx";
        try (FileInputStream inputStream = new FileInputStream(fileUrl)) {
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                if (row.getRowNum() == 0) {
                    continue;
                }

                Teacher teacher = new Teacher();

                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(3).setCellType(Cell.CELL_TYPE_NUMERIC);

                teacher.setT_username(row.getCell(0).getStringCellValue());
                teacher.setT_pwd(row.getCell(1).getStringCellValue());
                teacher.setT_name(row.getCell(2).getStringCellValue());
                teacher.setT_manager((int) row.getCell(3).getNumericCellValue());
                teacherMapper.insTeacher(teacher);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
